package com.c1.client.menu;

import com.c1.client.Frame;
import com.c1.client.menu.item.*;
import com.c1.client.menu.item.num.NUM1_MItem;
import com.c1.client.menu.item.num.NUM2_MItem;
import com.c1.client.menu.item.num.NUM3_MItem;
import com.c1.client.pan.Pan;
import com.c1.client.pan.Pans;
import com.google.common.collect.Lists;
import com.s.S;
import com.s.core.root.data._AllData;
import lombok.Data;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Optional;

@Data
public class MenuManager implements ActionListener {

    private MenuManager() {
        init();
    }

    private static volatile MenuManager me;

    public static MenuManager me() {
        if (me == null) {
            synchronized (MenuManager.class) {
                if (me == null) {
                    me = new MenuManager();
                }
            }
        }
        return me;
    }

    private JMenuBar menuBar;

    private static List<MItem> wmItems;
    ;
    private static W_MItem wMItem = new W_MItem();
    private static E_MItem eMItem = new E_MItem();
    private static F_MItem fMItem = new F_MItem();
    private static V_MItem vMItem = new V_MItem();
    private static C_MItem cMItem = new C_MItem();
    private static R_MItem rMItem = new R_MItem();
    private static NUM1_MItem num1MItem = new NUM1_MItem();
    private static NUM2_MItem num2MItem = new NUM2_MItem();
    private static NUM3_MItem num3MItem = new NUM3_MItem();

    private static A_MItem aMItem = new A_MItem();
    private static S_MItem sMItem = new S_MItem();
    private static G_MItem gMItem = new G_MItem();
    private static D_MItem dMItem = new D_MItem();
    private static X_MItem xMItem = new X_MItem();
    private static Z_MItem zMItem = new Z_MItem();
    private static T_MItem tMItem = new T_MItem();
    private static Q_MItem qMItem = new Q_MItem();
    private static CTRL_Q_MItem ctrlQMItem = new CTRL_Q_MItem();

    static {
        wmItems = Lists.newArrayList();
        wmItems.add(wMItem);
        wmItems.add(eMItem);
        wmItems.add(fMItem);
        wmItems.add(vMItem);
        wmItems.add(cMItem);
        wmItems.add(rMItem);
        wmItems.add(num1MItem);
        wmItems.add(num2MItem);
        wmItems.add(num3MItem);
        wmItems.add(aMItem);
        wmItems.add(sMItem);
        wmItems.add(gMItem);
        wmItems.add(dMItem);
        wmItems.add(xMItem);
        wmItems.add(zMItem);
        wmItems.add(tMItem);
        wmItems.add(qMItem);
        wmItems.add(ctrlQMItem);
    }

    private void init() {
        menuBar = new JMenuBar();

        JMenu opJMenu = new JMenu("操作");
        menuBar.add(opJMenu);

        opJMenu.add(wMItem.get(this));
        opJMenu.add(eMItem.get(this));
        opJMenu.add(fMItem.get(this));
        opJMenu.add(rMItem.get(this));

        JMenu switchJMenu = new JMenu("切换");
        opJMenu.add(switchJMenu);
        switchJMenu.add(vMItem.get(this));
        switchJMenu.add(cMItem.get(this));
        switchJMenu.add(sMItem.get(this));
        switchJMenu.add(dMItem.get(this));
        switchJMenu.add(gMItem.get(this));

        JMenu selectJMenu = new JMenu("选股");
        JMenu selfJMenu = new JMenu("自选");
        selectJMenu.add(selfJMenu);
        selfJMenu.add(num1MItem.get(this));
        selfJMenu.add(num2MItem.get(this));
        selfJMenu.add(num3MItem.get(this));
        selfJMenu.add(aMItem.get(this));
        selectJMenu.add(xMItem.get(this));
        selectJMenu.add(zMItem.get(this));
        selectJMenu.add(tMItem.get(this));
        selectJMenu.add(qMItem.get(this));
        selectJMenu.add(ctrlQMItem.get(this));
        menuBar.add(selectJMenu);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (Frame.LOADING_TITLE.equals(Frame.me().getTitle())) {
            return;
        }
        String command = e.getActionCommand();
        Pan pan = Pans.me().getCurrentPan();

        Optional<MItem> optionalMItem = wmItems.stream().filter(wMItem -> wMItem.showName().equals(command)).findFirst();
        if (optionalMItem.isPresent()) {
            MItem mItem = optionalMItem.get();
            if (mItem.needCodePics() && pan.code(true) == null) {
                return;
            }
            mItem.process();
        }
    }

    public void showStocksByCodes(List<String> codes, String date) {
        new Thread(() -> {
            String oldTitle = Frame.me().getTitle();
            Frame.me().setTitle(Frame.LOADING_TITLE);

            List<_AllData> allDataList = S.serverData.data(codes, date);
            if (allDataList.isEmpty()) {
                Frame.me().setTitle(oldTitle);
                return;
            }
            Pans.me().clear();
            Pans.me().removeAll();
            int index = 0;
            for (_AllData allData : allDataList) {
                Pan pan = new Pan(allData, index + 1);
                Pans.me().add(allData.d.code, pan.getFatherScrollPan());
                index++;
            }
            Pans.me().updateUI();
            Frame.me().setTitle(Pans.me().titleMessage());
        }).start();
    }
}
